package com.example.musicrecommend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.mapper.TeacherMapper;
import com.example.musicrecommend.model.dto.teacher.TeacherQueryRequest;
import com.example.musicrecommend.model.entity.Teacher;
import com.example.musicrecommend.service.TeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService{

    @Resource
//    private AnswerMapper answerMapper;
    private TeacherMapper teacherMapper;

    @Override
    public QueryWrapper<Teacher> getQueryWrapper(TeacherQueryRequest teacherQueryRequest) {
        if (teacherQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long teacher_id = teacherQueryRequest.getId();
        String teacher_name = teacherQueryRequest.getTeacher_name();

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(teacher_id != null && teacher_id != 0, "id", teacher_id);
        queryWrapper.eq(StringUtils.isNotBlank(teacher_name), "teacher_name", teacher_name);
        return queryWrapper;
    }

    @Override
    public void validTeacher(Teacher teacher, boolean isAdd) {
        ThrowUtils.throwIf(teacher == null, ErrorCode.PARAMS_ERROR);
        Long teacher_id = teacher.getId();
        String teacher_name = teacher.getTeacher_name();
        String teacher_sex = teacher.getTeacher_sex();
        String teacher_year = teacher.getTeacher_year();
        String teacher_class = teacher.getTeacher_class();
        String teacher_quanxian = teacher.getTeacher_quanxian();
        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(teacher_name,teacher_sex), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(teacher_id <= 0, ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isNotBlank(teacher_year) && teacher_year.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "年龄错误");
        }
        if (StringUtils.isNotBlank(teacher_class) && teacher_class.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "班级字数过长");
        }
        if (StringUtils.isNotBlank(teacher_quanxian) && teacher_quanxian.length() > 10) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "章节字数过长");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTeacher(Long[] teacher_ids) {
        if (teacher_ids == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        for (Long id : teacher_ids) {
            if (id == 0) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            // 删除
            boolean removeQuestionResult = this.removeById(id);
            if (!removeQuestionResult) {
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
            }
        }
        return true;
    }

}




